#include<string>
#include<iostream>
using namespace std;
class Solution {
public:
    int monotoneIncreasingDigits(int N) {
        int res = 0;
        string str = to_string(N);
        int len = str.length();
        for (int i = 1; i < len; ++i) {
            if (str[i] < str[i - 1]) {
                int tmp = pow(10, len - i);
                N = (N / tmp) * tmp - 1;
                str[i - 1] -= 1;
                for (int j = i - 1; j > 0; --j) {
                    if (str[j] < str[j - 1]) {
                        tmp = pow(10, len - j);
                        N = (N / tmp) * tmp - 1;
                        str[j - 1] -= 1;
                    }
                }
                break;
            }
        }
        return N;
    }
};

